package studio.raptor.ddal.dashboard.log.queue.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import studio.raptor.ddal.dashboard.log.common.LogServerConstant;
import studio.raptor.ddal.dashboard.log.queue.interfaces.IMessageQueue;
import studio.raptor.ddal.dashboard.log.queue.interfaces.IQueueManager;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@Scope("singleton")
public class JavaQueueManager implements IQueueManager {

	private boolean initFlag =false;

	private Map queueMap;

	@Autowired
	public JavaQueueManager(){
		queueMap=new ConcurrentHashMap();
	}

	/**
	 * 队列管理器初始化
	 */
	public void init(){
		if(initFlag==false){
			//对不同类型数据库所对应的队列初始化
			for(int m = 0; m< LogServerConstant.DATABASETYPES.length; m++){
				String databaseType = LogServerConstant.DATABASETYPES[m];
				if(databaseType!=null&&!databaseType.equals("")){
					databaseType = databaseType.toUpperCase();
					JavaMessageQueue frontdataQueue = new JavaMessageQueue();
					queueMap.put(LogServerConstant.LOG_SERVER_FRONT_DATA_QUEUE_NAME+"_"+databaseType, frontdataQueue);
				}
			}
			initFlag=true;
		}
	}

	public Object readObject() {
		return null;
	}

	public Object readObject(String queueName) {
		return ((JavaMessageQueue)queueMap.get(queueName)).readObject();
	}

	public int getQueueLength(String queueName){
		return ((JavaMessageQueue)queueMap.get(queueName)).getLength();
	}

	public void writeObject(Object object) {
	}

	public void writeObject(String queueName,Object object) {
		((JavaMessageQueue)queueMap.get(queueName)).writeObject(object);
	}

	public IMessageQueue getQueue(String queueName){
		return (IMessageQueue)queueMap.get(queueName);
	}

	public Map getQueueMap() {
		return queueMap;
	}

	public void setQueueMap(Map queueMap) {
		this.queueMap = queueMap;
	}

}
